package org.example.netty.zdyxieyi;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;

@Slf4j
class MessageSimpleChannelInboundHandler extends SimpleChannelInboundHandler<Message> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Message message) throws Exception {
        log.info("收到服务器的登录回应：{}", message);
        log.info("登录成功");
        //这里接收到登录成功的回应后，要通知下面的子线程，可以通过CountDownLatch来实现
    }

    @Override
    public void channelActive(final ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            log.info("请输入姓名：");
            String userName = scanner.nextLine();
            log.info("请输入年龄：");
            String age = scanner.nextLine();
            ctx.writeAndFlush(new LoginMessage(userName, Integer.parseInt(age)));

            while (true) {
                //在这里通过scanner阻塞获取用户输入
            }
        }).start();
    }
}
